-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds option value decoder utility functions #2425
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This comment has been minimized.
This comment has been minimized.
Oxygen deployed a preview of your
Learn more about Hydrogen's GitHub integration. |
27278e0
to
1cd0d69
Compare
Co-authored-by: Aaron Richner <aaron.richner@shopify.com>
19c87b1
to
060f6fe
Compare
Closed - Will be included as part of new Product form PR #2482 |
Reopen - The other PR is is being delayed |
Co-authored-by: Aaron Richner <aaron.richner@shopify.com>
…-decoder' into lh-add-option-value-decoder
wizardlyhel
approved these changes
Oct 2, 2024
juanpprieto
approved these changes
Oct 3, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing, thanks!
rbshop
reviewed
Oct 9, 2024
...es/hydrogen-react/src/optionValueDecoder.isOptionValueCombinationInEncodedVariant.example.js
Outdated
Show resolved
Hide resolved
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
WHY are these changes introduced?
Resolves https://github.com/Shopify/storefront-renderer/issues/25943
Co-authored by @aaronrichner .
WHAT is this pull request doing?
This PR adds a decoder + helper functions that may be used to parse the trie returned by product.encodedVariantExistence and product.encodedVariantAvailability fields. (Note -- at the time of this PR,
encodedVariantAvailability
is not yet shipped: https://github.com/Shopify/storefront-renderer/pull/28483 and we should likely defer merging this until it does.) Both of these fields are currently intended to be shipped in the 10-2024 release.Encoded option values
enable clients to obtain a full picture of which variants exist or have availability for a product. This is especially useful for products that may have very large variant sets (increased variants, combined listings), since the amount of data required to query and generate this information on the client may be significant.The encoding logic is identical for both
encodedVariantExistence
andencodedVariantAvailability
.How does option value encoding work?
The encoded string represents arrays of option value indices, structured as an encoded trie. The encoding strategy allows information about extremely large numbers of variants to be expressed in an extremely compact data structure
A value present in the encoding for
encodedVariantExistence
indicates that the product has a variant for that option value combination. ForencodedVariantAvailability
, presence indicates that the variant for the option value combination is available to be purchased. For example, a product with options[{ name: "color", values: ["red", "green'] }, { name: "size", values: ["small", "medium'] }, { name: "material", values: ["cotton", "linen'] }]
may only have variants forred/small/cotton
andgreen/medium/linen
. In this case, theencodedVariantExistence
string would contain (packed) entries for the following option value indices:[ [0, 0, 0], [1, 1, 1] ]
.V1 of the encoding strategy below:
Integers represent option and values, so [0,3] represents option_value at array index 0 for the option at array index 0 and option_value at array index 3 for the option at array index 1.
:
,,
,-
are control characters.:
indicates a new option. ex: 0:1 indicates value 0 for the option in position 1, value 1 for the option in position 2.,
indicates the end of a repeated prefix, multiple consecutive commas indicate the end of multiple repeated prefixes.-
indicates a continuous range of option values. ex: 0 1-3 4Encoding process for
encodedVariantExistence
:Example options: [Size, Color, Material]
Example values: [[Small, Medium, Large], [Red, Blue], [Cotton, Wool]]
Existing variants: [Small, Red, Cotton] (0:0:0), [Small, Blue, Cotton] (0:1:0), [Small, Blue, Wool] (0:1:1), [Medium, Red, Cotton] (1:0:0), [Medium, Red, Wool] (1:0:1), [Medium, Blue, Wool] (1:1:1), [Large, Red, Wool] (2:0:1), [Large, Blue, Cotton] (2:1:0)
Option value indices for existing variants: [[0,0,0], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,1], [2,0,1], [2,1,0]]
Step 1: Encode as string: "0:0:0,0:1:0,0:1:1,1:0:0,1:0:1,1:1:1,2:0:1,2:1:0,"
Step 2: Combine nodes that share a prefix: "0:0:0,0:1:0 1,1:0:0 1,1:1:1,2:0:1,2:1:0,"
Step 3: Encode data as a trie so no prefixes need to be repeated: "0:0:0,1:0 1,,1:0:0 1,1:1,,2:0:1,1:0,,"
Step 4: Since the options are sorted, use a dash to express ranges: "0:0:0,1:0-1,,1:0:0-1,1:1,,2:0:1,1:0,,"
Checklist